/*
 * XXX
 */

package edu.uhcl.csci4838.group3.pendingIntents.ringer;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ComponentInfo;
import android.media.AudioManager;
import android.util.Log;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper;
import edu.uhcl.csci4838.group3.database.BackpackEduDatabaseHelper.CursorCourse;
import edu.uhcl.csci4838.group3.database.info.Course;
import edu.uhcl.csci4838.group3.pendingIntents.IntentExtraStringStorage;
import edu.uhcl.csci4838.group3.pendingIntents.WakeUpReminderIntentService;

/**
 * Handles changing the ringer
 * 
 * @author Tyler Hays
 */
public class RingerChange extends WakeUpReminderIntentService implements
		IntentExtraStringStorage {
	private static final String TAG = "RingerChange";
	
	/**
	 * Changes ringer when class ends
	 * 
	 * @param ringerMode
	 *            ringer mode in settings
	 * @param preCourseRingMode
	 *            what the ringer mode is before class started
	 * @param audioManger
	 *            Audio Manger
	 */
	private static void ringerClassEnd(final int ringerMode,
			final int preCourseRingMode,
			final AudioManager audioManger) {
		int currentRingMode;
		currentRingMode = audioManger.getRingerMode();
		if (currentRingMode != ringerMode) {
			Log.e(RingerChange.TAG,
					"Ring change during class");
		} else {
			audioManger.setRingerMode(preCourseRingMode);
		}
		
	}
	
	/**
	 * constuctuor
	 * 
	 * @param name
	 *            see super
	 */
	public RingerChange() {
		super("RingerChange");
	}
	
	/**
	 * does the ringer change work
	 * 
	 * @param intent
	 *            {@inheritDoc}
	 */
	/* 
	 * (non-Javadoc)
	 * 
	 * @see edu.uhcl.csci4838.group3.pendingIntents.WakeUpReminderIntentService#doReminderWork(android.content.Intent)
	 */
	@Override
	public void doReminderWork(final Intent intent) {
		final AudioManager audioManger = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
		int changeRingMode = 0;
		int preCourseRingMode = 0;
		final boolean classBegin = intent.getBooleanExtra(IntentExtraStringStorage.CLASS_BEGIN,
				false);
		audioManger.getMode();
		Log.d(TAG, "WORKING");
		
		final SharedPreferences sharePref = this.getSharedPreferences("AppPreferences",
				Context.MODE_PRIVATE);
		final int sound = sharePref.getInt("soundSettings",
				0);
	
		switch (sound) {
			case 0:
				Log.d(RingerChange.TAG,
						"not change in ringer");
				break;
			
			case 1:
				Log.d(RingerChange.TAG,
						"vibrate");
				changeRingMode = AudioManager.RINGER_MODE_VIBRATE;
				break;
			case 2:
				Log.d(RingerChange.TAG,
						"silent");
				changeRingMode = AudioManager.RINGER_MODE_SILENT;
				break;
			
			default:
				changeRingMode = AudioManager.RINGER_MODE_NORMAL;
				Log.wtf(RingerChange.TAG,
						"default ran");
				break;
		}
		if (!classBegin) {
			preCourseRingMode = intent.getIntExtra(IntentExtraStringStorage.RINGER_MODE,
					-1);
			Log.e(TAG, "Shoudl not run");
			RingerChange.ringerClassEnd(changeRingMode,
					preCourseRingMode,
					audioManger);
		} else {
			this.ringerClassBegin(intent,
					sound,
					changeRingMode,
					audioManger);
		}
		
	}
	
	/**
	 * changes ringer for when class begins
	 * 
	 * @param intent
	 *            the intent
	 * @param sound
	 *            setting for what the choice in settings is
	 * @param changeRingMode
	 *            the mode to change the ringer to during class
	 * @param audioManger
	 *            Audio Manger
	 */
	private void ringerClassBegin(final Intent intent,
			final int sound,
			final int changeRingMode,
			final AudioManager audioManger) {
		
		AlarmManager mAlarmManager;
		BackpackEduDatabaseHelper dbhelper;
		
		final ContentValues contentValues = new ContentValues();
		CursorCourse course;
		Log.d (TAG, "Change ringer class beging");
		final RingerIntent ringerIntent = new RingerIntent(this);
		Intent i;
		PendingIntent pi;
		
		final Calendar when = Calendar.getInstance();
		
		int currentSoundMode;
		int minutes;
		int hours;
		long rowID;
		
		mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
		
		/*
		 * code below is to set up the intent for when 
		 * the class starts the next time
		 */
		dbhelper = new BackpackEduDatabaseHelper(this);
		rowID = intent.getLongExtra(IntentExtraStringStorage.ROW_ID,
				-1);
		contentValues.put(Course.ID,
				Long.valueOf(rowID));
		course = dbhelper.getCourse(contentValues,
				null,
				true);
		ringerIntent.setUpRingerChange(course);
		course.close();
		dbhelper.close();
		
		/*
		 * If the current sound setting is lower than preference mode
		 * then it will not change the setting nor set up an pending intent
		 * change it back
		 */
		currentSoundMode=audioManger.getMode();
		
		if (sound!=0) {
			Log.d(TAG, "RAN");
			
			audioManger.setRingerMode(changeRingMode);

			i = new Intent(this,
					AlarmManager.class);
			i.putExtra(IntentExtraStringStorage.TABLE_NAME,
					IntentExtraStringStorage.RINGER_CHANGE);
			
			//Get and set the time the class ended
			hours = intent.getIntExtra(IntentExtraStringStorage.CLASS_END_HR,
					0);
			minutes = intent.getIntExtra(IntentExtraStringStorage.CLASS_END_MIN,
					0);
			when.set(Calendar.MINUTE,
					minutes);
			when.set(Calendar.HOUR_OF_DAY,
					hours);
			
			i.putExtra(IntentExtraStringStorage.RINGER_MODE,
					audioManger.getMode());
			i.putExtra(IntentExtraStringStorage.CLASS_BEGIN,
					false);
			
			pi = PendingIntent.getBroadcast(this,
					0,
					i,
					PendingIntent.FLAG_ONE_SHOT);
			
			//change ringer back when class ends
			mAlarmManager.set(AlarmManager.RTC_WAKEUP,
					when.getTimeInMillis(),
					pi);
			Log.d(TAG, when.getTime().toLocaleString());
		}
		else
		{
				Log.d(TAG, "Ringer did not change");
		}
		
	}
}
